/**
 * Function applyTableSum
 * 
 * 函数功能： 为表格添加合计行,本函数依赖于jquery
 * 
 * @param {}
 *            table : The table which append the sum row to 需要添加合计行的表格对象
 * @param {}
 *            colArr : The col index Arry which shuld sum 需要合计的列序号数组，从0开始
 * @param {}
 *            capIndex : caption index defalt is 0 合计标题放置的列，默认是0。即第一列
 * @param {}
 *            emptyText : The text not sumed col default is '-' 未合计行的文本
 */
function applyTableSum(table, colArr, capIndex, emptyText) {
	var me = this;
	var _capIndex = capIndex || 0;
	var _emptyText = emptyText || "-";
	var _SD = "$", _hasD = new Array();
	var _SY = "￥", _hasY = new Array();
	var _comma = ",", _hasComma = new Array();
	if (table != null && table != undefined && colArr != null) {
		var lastTr, sumHtml;

		var iColData=0, strData;
		var trs = table.children("tbody").first().children("tr");
		var trLen = trs.length;
		var lastTr = trs.last();
		var tds, tdLen, tr, td;
		var i, j, k;
		sumHtml = "<tr class='td_row'>";
		var sumCols = new Array(trLen);
		// 遍历所有行
		for (i = 0; i < trLen; i++) {
			tr = trs.get(i);
			tr = JQ(tr);
			tds = tr.children("td");
			if (tds == null || tds == undefined) {
				continue;
			}
			tdLen = tds.length;
			for (j = 0, k = 0; j < tdLen; j++) {
				td = tds.get(j);
				td = JQ(td);
				// 如果是要统计的列
				if (colArr[k] == j) {
					strData = td.text();
					if(strData==null||strData==undefined||strData=="")
					{
						strData="0";
					}
					// console.log("strData="+strData);
					while (strData.indexOf(_SD) != -1) {
						strData = strData.replace(_SD, "");
						_hasD[j] = true;
					}
					while (strData.indexOf(_SY) != -1) {
						strData = strData.replace(_SY, "");
						_hasY[j] = true;
					}
					while (strData.indexOf(_comma) != -1) {
						strData = strData.replace(_comma, "");
						_hasComma[j] = true;
					}
					try {
						iColData = parseFloat(strData);						
						if (sumCols[j] == null || sumCols[j] == undefined) {
							sumCols[j] = 0;
						}
						sumCols[j] = parseFloat(sumCols[j])+iColData;
					} catch (e) {
						console.log(e.message);
					}
					k++;
				}
			}
		}// end for
		// build sum td
		var m = 0;
		for (m = 0, k = 0; m < tdLen; m++) {
			if (m == _capIndex) {
				sumHtml += "<td>合计</td>";
			} else if (colArr[k] == m) {
				if (sumCols[m] != null && sumCols[m] != undefined) {
					var iSum = sumCols[m];
					if (_hasD[m] == true || _hasY[m] == true
							|| _hasComma[m] == true) {
						iSum = iSum.toFixed(2);
					}
					var str = sumCols[m] + "";
					if (_hasComma.length >= m && _hasComma[m] == true) {
						str = Convert(iSum);
					}
					if (_hasD.length >= m && _hasD[m] == true) {
						str = _SD + str;
					} else if (_hasY.length >= m && _hasY[m] == true) {
						str = _SY + str;
					}
					sumHtml += "<td>" + str + "</td>";
				} else {
					sumHtml += "<td>" + _emptyText + "</td>";
				}
				k++;
			} else {
				sumHtml += "<td>" + _emptyText + "</td>";
			}
		}
		sumHtml += "</tr>";
		lastTr.after(sumHtml);
	}
};
function Convert(money) {
	// console.log("money="+money);
	var s = money; // 获取小数型数据
	s += "";
	if (s.indexOf(".") == -1)
		s += ".0"; // 如果没有小数点，在后面补个小数点和0
	if (/\.\d$/.test(s))
		s += "0"; // 正则判断
	while (/\d{4}(\.|,)/.test(s))
		// 符合条件则进行替换
		s = s.replace(/(\d)(\d{3}(\.|,))/, "$1,$2"); // 每隔3位添加一个
	// console.log("converted="+s);
	return s;
}